<template>
  <div class="register-container">
    <div class="register-box">
      <h1 class="register-title">用户注册</h1>
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            v-model="form.username"
            id="username"
            type="text"
            required
            placeholder="请输入用户名"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            v-model="form.password"
            id="password"
            type="password"
            required
            placeholder="请输入密码"
          />
        </div>

        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            v-model="form.confirmPassword"
            id="confirmPassword"
            type="password"
            required
            placeholder="请再次输入密码"
          />
        </div>

        <button type="submit" class="register-btn">注 册</button>
        
        <div class="register-footer">
          <p>已有账号？<router-link to="/login">立即登录</router-link></p>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { useUserStore } from '../stores/user'

const form = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})
const router = useRouter()
const authStore = useAuthStore()
const userStore = useUserStore()

const handleRegister = async () => {
  // 验证逻辑保持不变
  if (!form.username || !form.password) {
    alert('用户名和密码不能为空')
    return
  }
  if (form.password !== form.confirmPassword) {
    alert('两次输入的密码不一致')
    return
  }
  
  try {
    const result = authStore.register(form.username, form.password)
    if (result.success) {
      userStore.setUser(result.user) // 保存用户数据
      alert('注册成功！请登录')
      router.push('/login') // 改为跳转到登录页
    } else {
      alert(result.message || '注册失败')
    }
  } catch (error) {
    alert('注册过程中发生错误: ' + error.message)
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.register-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.register-title {
  margin: 0 0 30px;
  color: #333;
  text-align: center;
  font-size: 28px;
  font-weight: 600;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  color: #555;
  font-size: 14px;
  font-weight: 500;
}

.form-group input {
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s;
}

.form-group input:focus {
  border-color: #2575fc;
  outline: none;
  box-shadow: 0 0 0 3px rgba(37, 117, 252, 0.2);
}

.register-btn {
  padding: 12px;
  background: linear-gradient(to right, #6a11cb, #2575fc);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.register-btn:hover {
  opacity: 0.9;
  transform: translateY(-2px);
}

.register-footer {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.register-footer a {
  color: #2575fc;
  text-decoration: none;
  font-weight: 500;
}

.register-footer a:hover {
  text-decoration: underline;
}
</style>